/************************************************************************/
/* GUI                                                                  */
/************************************************************************/
#include "GUI//Panels/TowerInfoPanel.h"

namespace TD_GUI {



	/************************************************************************/
	/* Default definitions                                                  */
	/************************************************************************/
	const string TowerInfoPanel::m_towerInfoBackground = "GUI/Panels/towerInfo.png";
	const Coord2D TowerInfoPanel::m_infoPanelPosition(0.8026*screenWidth, 0.315*screenHeight);
	const Coord2D TowerInfoPanel::m_infoPanelSize(0.1862*screenWidth, 0.5025*screenHeight);


	/************************************************************************/
	/* Private functions                                                    */
	/************************************************************************/
	int TowerInfoPanel::upgradeButtonOnClick(int mouseButton, const Coord2D & mousePos) {
		return 0;
	}

	void TowerInfoPanel::initTextboxes() {

		m_pTowerName = new TextMessage(m_infoPanelPosition + Coord2D(0.0139*screenWidth, 0.00625*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, Color::WHITE);


		m_pTowerPrice = new TextMessage(m_infoPanelPosition + Coord2D(0.08379*screenWidth, 0.19375*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, m_defaultFontColor);
		m_pTowerUpgradePrice = new TextMessage(m_infoPanelPosition + Coord2D(0.107*screenWidth, 0.19375*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, Color::RED);

		m_pTowerDamage = new TextMessage(m_infoPanelPosition + Coord2D(0.08379*screenWidth, 0.25625*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, m_defaultFontColor);
		m_pTowerUpgradeDamage = new TextMessage(m_infoPanelPosition + Coord2D(0.107*screenWidth, 0.25625*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, Color::RED);

		m_pTowerRange = new TextMessage(m_infoPanelPosition + Coord2D(0.08379*screenWidth, 0.31875*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, m_defaultFontColor);
		m_pTowerUpgradeRange = new TextMessage(m_infoPanelPosition + Coord2D(0.107*screenWidth, 0.31875*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, Color::RED);

		m_pTowerCooldown = new TextMessage(m_infoPanelPosition + Coord2D(0.08379*screenWidth, 0.38375*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, m_defaultFontColor);
		m_pTowerUpgradeCooldown = new TextMessage(m_infoPanelPosition + Coord2D(0.107*screenWidth, 0.38375*screenHeight), "0", 
			m_defaultFont, m_defaultFontSize, Color::RED);

		m_pTowerSellPrice = new TextMessage(m_infoPanelPosition + Coord2D(0.146*screenWidth, 0.4475*screenHeight), "0", 
			m_defaultFont, 20, Color::BLACK);

	}

	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	TowerInfoPanel::TowerInfoPanel() : BasePanel(m_infoPanelPosition, m_infoPanelSize) {

		m_pImage = TD_Resources::ResourceManager::loadImage(m_towerInfoBackground);

		initTextboxes();


		m_pUpgradeButton = new BaseButton(Coord2D(0.8137*screenWidth, 0.7562*screenHeight), Coord2D(0.069832*screenWidth, 0.0325*screenHeight),
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/upgradeButton.png"));
		m_pSellButton = new BaseButton(Coord2D(0.89106*screenWidth, 0.7562*screenHeight), Coord2D(0.08472*screenWidth, 0.0325*screenHeight),  
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/sellButton.png"));
		/*m_pSellButton = new BaseButton(Coord2D(971-14, 0.7562*screenHeight), Coord2D(75, 26),  
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/sellButton.png"));*/

		m_pUpgradeButton->setMouseClickedCallback(&TowerInfoPanel::upgradeButtonOnClick);

		m_showButtons = false;

		m_pTower = NULL;
		m_towerDescription = NULL;

	}

	TowerInfoPanel::TowerInfoPanel(const TowerInfoPanel &rhs) {
		
	}

	TowerInfoPanel & TowerInfoPanel::operator =(const TowerInfoPanel & rhs) {
		return(*this);
	}

	TowerInfoPanel::~TowerInfoPanel() {
		delete m_pTowerPrice;
		delete m_pTowerDamage;
		delete m_pTowerRange;
		delete m_pTowerCooldown;

		/** Buttons: */
		delete m_pUpgradeButton;
		delete m_pSellButton;
	}

	/************************************************************************/
	/* Rendering                                                            */
	/************************************************************************/

	void TowerInfoPanel::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		BasePanel::render(g_destSurface, deltaTime);
		m_pTowerName->render(g_destSurface, deltaTime);
		if(m_towerDescription != NULL) {
			m_towerDescription->render(g_destSurface, deltaTime);
		}

		m_pTowerPrice->render(g_destSurface, deltaTime);
		m_pTowerDamage->render(g_destSurface, deltaTime);
		m_pTowerRange->render(g_destSurface, deltaTime);
		m_pTowerCooldown->render(g_destSurface, deltaTime);

		if(m_pTower && m_pTower->getTowerLevel() < NUMBER_OF_LEVELS) {
			if(m_showButtons == true) {
				m_pTowerUpgradePrice->render(g_destSurface, deltaTime);
				m_pTowerUpgradeDamage->render(g_destSurface, deltaTime);
				m_pTowerUpgradeRange->render(g_destSurface, deltaTime);
				m_pTowerUpgradeCooldown->render(g_destSurface, deltaTime);
				m_pUpgradeButton->render(g_destSurface, deltaTime);
				m_pSellButton->render(g_destSurface, deltaTime);
				m_pTowerSellPrice->render(g_destSurface, deltaTime);
			}
		}

		
		

	}

	interaction_t TowerInfoPanel::processEvent(const SDL_Event &event) {
		interaction_t interaction;
		interaction.type = noInteraction;

		if(event.type == SDL_MOUSEBUTTONDOWN ) { //If the left mouse button was pressed 
			Coord2D mousePos;
			mousePos.x = event.button.x;
			mousePos.y = event.button.y;
			if(m_showButtons && m_pTower && m_pTower->getTowerLevel() < NUMBER_OF_LEVELS) {
				if(m_pUpgradeButton->collisionCheck(mousePos)) {
					m_pUpgradeButton->click(event.button.button, mousePos);
					interaction.type = towerUpgrade;
				} else if(m_pSellButton->collisionCheck(mousePos)) {
					interaction.type = towerSell;
					interaction.value = m_pTower->getID();
				}
			}
		} else if(event.type == SDL_MOUSEMOTION) { //Mouse moved
			Coord2D mousePos;
			mousePos.x = event.button.x;
			mousePos.y = event.button.y;
			if(m_showButtons && m_pTower && m_pTower->getTowerLevel() < NUMBER_OF_LEVELS) {
				if(m_pUpgradeButton->collisionCheck(mousePos)) {
					SDLCursor::setCursor(handCursor);
					interaction.type = towerInfoEvent;
				} else if(m_pSellButton->collisionCheck(mousePos)) {
					SDLCursor::setCursor(handCursor);
					interaction.type = towerInfoEvent;
				} else {
					SDLCursor::setCursor(arrowCursor);
					interaction.type = towerInfoEvent;
				}
			}
		}

		return(interaction);

	}
}